<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isFull?'exit-fullscreen':'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFull: false
    }
  },
  mounted() {
    // 对于sec按键退出全屏的监听
    screenfull.on('change', () => {
      this.isFull = screenfull.isFullscreen
    })
    // document.querySelector('fullscreenchange', () => {
    //   // 使用两次取反，降其他的数据类型隐士转换boolean
    //   this.isFull = !!document.fullscreenElement
    // })
  },
  methods: {
    toggleScreen() {
      if (this.isFull) {
        document.exitFullscreen()
      } else {
        document.documentElement.requestFullscreen()
      }
      this.isFull = !this.isFull
    }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
